const JSONP = require('node-jsonp');
const httpRequest = require('request');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;

const csvWriter = createCsvWriter({
    path: './stocks.csv',
    header: [
        {id: 'symbol', title: '代码'},
        {id: 'name', title: '名称'},
        {id: 'price', title: '当前价'},
        {id: 'mktcap', title: '市值(亿)'},
        {id: 'pe', title: '市盈率'},
        {id: 'max', title: '最高价'},
        {id: 'maxupdown', title: '最大涨跌幅'},
        {id: 'lastday', title: '最远时间'}
    ]
});

let page = 1, allStocks = [];

global["callback"] = function (json) {
	cb(json);

	if (page <= 4) {
		page++;
		request();
	} else {
		handleDetailData();
	}
}

function request () {
	JSONP("http://money.finance.sina.com.cn/q/api/jsonp_v2.php/callback/US_ChinaStockService.getData?page=" + page + "&num=60&sort=mktcap&asc=0&market=&concept=0", "callback", cb)
}

function cb(data) {
	allStocks = allStocks.concat(data);
}

let stockIdx = 0;
function handleDetailData() {
	
	let begin = +new Date('2014-01-01 00:00:00'), end = +new Date('2019-06-01 00:00:00');
	//console.log('....symbol', allStocks[stockIdx].symbol);
	httpRequest({
		url: 'https://xueqiu.com/stock/forchartk/stocklist.json?symbol=' + allStocks[stockIdx].symbol + '&period=1month&type=before&begin=' + begin +'&end=' + end + '&_=' + (+Date.now()),
		headers: {
			'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36',
			'Cookie': '_ga=GA1.2.1197069232.1508895483; device_id=267e01aecea2f011daa6cf136537c5f1; s=eq1214x0do; aliyungf_tc=AQAAAO6BnWiDKgwANADL38+x16/F8hYQ; snbim_minify=true; __utmc=1; __utmz=1.1546421065.42.7.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); bid=09ffb40c408ad081815d87148a08ff82_jsobfewl; Hm_lvt_1db88642e346389874251b5a1eded6e3=1556153725,1556520252,1557129362,1557913887; _gid=GA1.2.1639632863.1558315649; remember=1; remember.sig=K4F3faYzmVuqC0iXIERCQf55g2Y; xq_a_token=e10d458eabdf1b3748550d2400b116eb599088e5; xq_a_token.sig=OWJKfdjq6kZ5rMgQczKJvvpz1wA; xqat=e10d458eabdf1b3748550d2400b116eb599088e5; xqat.sig=x28JffGgu7Cm4tJk-RtK3_9r0GA; xq_r_token=b077da7f079e06d99adc368a65fb6d4fefd0e2c2; xq_r_token.sig=NsNa8LEatW-sSb6mowISg0yvCeg; xq_is_login=1; xq_is_login.sig=J3LxgPVPUzbBg3Kee_PquUfih7Q; u=5583657325; u.sig=fQC6TU0KzV4jiqlxfpFiYghMUC0; __utma=1.1197069232.1508895483.1558515757.1558600169.110; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1558687082'
		}
	}, function (error, response, body) {
		let data = JSON.parse(body);
		if (data.success === "true") {
			let chartlist = data.chartlist, maxPrices = [], maxPrice = null ,
				curPrice = chartlist[chartlist.length - 1].close;

			chartlist.forEach(detail => {
				maxPrices.push(detail.high);
			});

			maxPrice = Math.max.apply(null, maxPrices);

			let upDown = (curPrice - maxPrice) / maxPrice * 100;

			allStocks[stockIdx].max = maxPrice;
			allStocks[stockIdx].maxupdown = upDown.toFixed(2);
			allStocks[stockIdx].lastday =  chartlist[0].time;
			allStocks[stockIdx].mktcap = allStocks[stockIdx].mktcap / 100000000;

			console.log('detail=>', stockIdx, allStocks[stockIdx]);

			stockIdx++

			if (stockIdx < allStocks.length && allStocks[stockIdx]) {
				setTimeout(handleDetailData, 100);
			} else {
				csvWriter.writeRecords(allStocks.filter(s => s && s.symbol)).then(() => {
			        console.log('...Done');
			    });
			}
		}

	});
}

request();



